<?php
declare (strict_types=1);

namespace app\school\service\school\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\school\logic\school\admin\School as SchoolLogic;
use app\school\logic\school\admin\SchoolLevel as SchoolLevelLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

/**
 * 机构管理-后台
 */
class School extends BaseService
{
    /**
     * 机构列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-27
     */
    public function index()
    {
        $map = [];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $category = Request::get('category/d', 0);
        $category && $map[] = ['category', 'like', '%,' . $category . ',%'];
        if (Request::has('status', 'get', true) && Request::get('status/d', -1) !== -1) {
            $map[] = ['status', '=', Request::get('status/d')];
        } else {
            // 搜索功能
            $map[] = ['status', 'in', '0,1'];
        }
        $data = app(SchoolLogic::class)->getList($map)->bindAttr('admin', ['phone']);
        // 定义字段
        $keyList = [
            'title'       => ListItem::make('custom')->title('机构')->width(180),
            'school_code' => ListItem::make()->title('机构编码'),
            'phone'       => ListItem::make()->title('机构管理员手机号'),
            'from_text'   => ListItem::make()->title('机构来源'),
            'mhm_cname'   => ListItem::make()->title('子域名'),
            'is_audit'    => ListItem::make('custom')->title('是否需要审核'),
            'status'      => ListItem::make('custom')->title('状态'),
        ];
        // 机构分类
        $category  = app(Category::class)->cascaderCate(0, 2);
        $searchKey = [
            'title'    => FormItem::make()->title(false)->placeholder('名称')->maxlength(30),
            'category' => FormItem::make('cascader')->title(false)->options($category)->placeholder('分类')->required(),
            'status'   => FormItem::make('select')->title(false)->placeholder('状态')->options([
                ['title' => '不限制', 'value' => -1],
                ['title' => '正常', 'value' => 1],
                ['title' => '禁止', 'value' => 0],
            ]),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 课程界面配置
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-26
     */
    public function keyListInit($create = true): array
    {
        // 机构分类
        $category = app(Category::class)->cascaderCate(0, 2);
        // 机构等级
        $levels = app(SchoolLevelLogic::class)->getSelectOptions();

        // 定义字段
        return [
            'title'                 => FormItem::make()->title('名称')->required(),
            'cates'                 => FormItem::make('cascader')->title('分类')->options($category)->multiple(true)->required(),
            'logo'                  => FormItem::make('custom')->title('LOGO')->required()->help('建议尺寸 150px*40px'),
            'level_id'              => FormItem::make('select')->title('等级')->options($levels)->required(),
            'admin_user_phone'      => FormItem::make()->title('机构管理员')->placeholder('输入管理员手机号')->required()->help('手机号未注册管理员时,会自动注册成为管理员')->visible($create),
            'is_audit'              => FormItem::make('slideSwitch')->title('是否需要审核')->activeValue()->inactiveValue(0)->help('开启后，机构或机构讲师上传的点播课程资源将需要审核'),
            // 'best_sort'             => FormItem::make('inputNumber')->title('排序值'),
            'intro'                 => FormItem::make('textarea')->title('简介')->maxlength(40)->wordLimit(),
            'info'                  => FormItem::make('editor')->title('详细介绍'),
            'telephone'             => FormItem::make()->title('联系电话')->help('联系电话可以是座机号'),
            'address'               => FormItem::make()->title('联系地址'),
            'legal_IDcard'          => FormItem::make()->title('法人身份证号码'),
            'legal_IDcard_positive' => FormItem::make('custom')->title('法人身份证头像面')->help('建议尺寸 400px*250px'),
            'legal_IDcard_side'     => FormItem::make('custom')->title('法人身份证国徽面')->help('建议尺寸 400px*250px'),
            'business_license'      => FormItem::make('custom')->title('营业执照')->help('建议尺寸 840px*594px'),
            'status'                => FormItem::make('slideSwitch')->title('状态')->activeValue()->inactiveValue(0)->visible(!$create),
        ];
    }

    /**
     * 待审核机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-29
     */
    public function waiting()
    {
        $map = [
            ['status', '=', 2],
        ];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $data = app(SchoolLogic::class)->getWaitingList($map)->bindAttr('apply', ['apply_user_phone' => 'phone']);
        // 定义字段
        $keyList   = [
            'title'            => ListItem::make('custom')->title('机构'),
            'school_code'      => ListItem::make()->title('机构编码'),
            'apply_user_phone' => ListItem::make()->title('申请人手机号'),
            'from_text'        => ListItem::make()->title('申请来源'),
            'create_time'      => ListItem::make()->title('申请时间'),
        ];
        $searchKey = [
            'title' => FormItem::make()->title(false)->placeholder('名称')->maxlength(30),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 搜索机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-06
     *
     * @param string $keyword [description]
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function search(string $keyword = ''): Response
    {
        $map  = [
            ['status', '=', 1],
        ];
        $list = app(SchoolLogic::class)->getSearchList($keyword, 12, $map);

        return static::parseToData($list, 1, '获取成功');

    }

    /**
     * 批量禁用
     *
     * @return Response
     */
    public function disables(): Response
    {
        $ids = Request::put('ids/a');
        app(SchoolLogic::class)->batchUpdated($ids, 'status', 0);

        return static::parseToData([], 1, '操作成功');
    }

    /**
     * 批量启用
     *
     * @return Response
     */
    public function enables(): Response
    {
        $ids = Request::put('ids/a');
        app(SchoolLogic::class)->batchUpdated($ids, 'status', 1);

        return static::parseToData([], 1, '操作成功');
    }

    /**
     * 批量删除
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/10/16
     *
     * @param $ids
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function deletes($ids): Response
    {
        app(SchoolLogic::class)->deleteSchools($ids);

        return static::parseToData([], 1, '操作成功');
    }
}
